<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: options.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$full_header            = 1;
$body['class']          = 'row3';
$header[]['HEADER_TEXT']    = '<script language=JavaScript src="picker.js"></script>';
require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);

$sql    = "SELECT * FROM {$dbprefix}sessions WHERE session_id='{$_REQUEST['sid']}'";
$result = $db->sql_query($sql);
$row    = $db->sql_fetchrow($result);
if (!is_array($row)) {
    showFatalError("Please <a href=index.$phpEx>log back in</a> first.");
}

$sql    = "SELECT * FROM {$dbprefix}prefs WHERE user_id={$row['user_id']}";
$result = $db->sql_query($sql);
$p_row  = $db->sql_fetchrow($result);

if ($_REQUEST['user_id']) {
    if (is_array($p_row)) {
        $sql    = "UPDATE {$dbprefix}prefs
                SET
                    user_tagline='{$_REQUEST['user_tagline']}',
                    user_login_msg='{$_REQUEST['user_login_msg']}',
                    user_logout_msg='{$_REQUEST['user_logout_msg']}',
                    user_show_avatars='{$_REQUEST['user_show_avatars']}',
                    user_allow_pms='{$_REQUEST['user_allow_pms']}',
                    user_allow_ext_pms='{$_REQUEST['user_allow_ext_pms']}',
                    user_show_smilies='{$_REQUEST['user_show_smilies']}',
                    user_color='{$_REQUEST['user_color']}',
                    user_font='{$_REQUEST['user_font']}',
                    user_size='{$_REQUEST['user_size']}',
                    user_css='{$_REQUEST['user_css']}',
                    user_frames1='{$_REQUEST['user_frames1']}',
                    user_frames2='{$_REQUEST['user_frames2']}'
                WHERE
                    user_id={$row['user_id']}";
        $notice = "Your preferences have been updated."; 
    } else {
        $sql    = "INSERT INTO {$dbprefix}prefs
                    (user_id, user_tagline, user_login_msg, user_logout_msg, user_color, user_font, user_size, user_css, user_frames1, user_frames2)
                VALUES
                    ({$_REQUEST['user_id']}, '{$_REQUEST['user_tagline']}', '{$_REQUEST['user_login_msg']}', '{$_REQUEST['user_logout_msg']}', '{$_REQUEST['user_color']}', '{$_REQUEST['user_font']}', '{$_REQUEST['user_size']}', '{$_REQUEST['user_css']}', '{$_REQUEST['user_frames1']}', '{$_REQUEST['user_frames2']}')";
        $notice = "Your preferences have been created.";
    }
    $result = $db->sql_query($sql);
    //$err    = $db->sql_error();

    $sql    = "SELECT * FROM {$dbprefix}prefs WHERE user_id={$row['user_id']}";
    $result = $db->sql_query($sql);
    $p_row  = $db->sql_fetchrow($result);
}

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'options_body.tpl'));

$y_arr  = array('Y' => 'checked', 'N' => '');
$n_arr  = array('Y' => '', 'N' => 'checked');

$c_avatars  = ($p_row['user_show_avatars']  == 0 ? $y_arr : $n_arr);
$c_smilies  = ($p_row['user_show_smilies']  == 0 ? $y_arr : $n_arr);
$c_pms      = ($p_row['user_allow_pms'] == 0 ? $y_arr : $n_arr);
$c_ext_pms  = ($p_row['user_allow_ext_pms'] == 0 ? $y_arr : $n_arr);

$template->assign_vars(array(
    'USER_ID'               => $row['user_id'],
    'SID'                   => $_REQUEST['sid'],

    'USER_TAGLINE'          => $p_row['user_tagline'],
    'USER_LOGIN_MSG'        => $p_row['user_login_msg'],
    'USER_LOGOUT_MSG'       => $p_row['user_logout_msg'],
    'USER_COLOR'            => $p_row['user_color'],
    'USER_CSS'              => $p_row['user_css'],
    'USER_FRAMES1'          => $p_row['user_frames1'],
    'USER_FRAMES2'          => $p_row['user_frames2'],

    'USER_SHOW_AVATARS_Y'   => $c_avatars['Y'],
    'USER_SHOW_AVATARS_N'   => $c_avatars['N'],

    'USER_SHOW_SMILIES_Y'   => $c_smilies['Y'],
    'USER_SHOW_SMILIES_N'   => $c_smilies['N'],

    'USER_ALLOW_PMS_Y'      => $c_pms['Y'],
    'USER_ALLOW_PMS_N'      => $c_pms['N'],

    'USER_ALLOW_EXT_PMS_Y'  => $c_ext_pms['Y'],
    'USER_ALLOW_EXT_PMS_N'  => $c_ext_pms['N']
));

$fonts  = array('Arial', 'Comic Sans', 'Courier New', 'Garamond', 'Georgia', 'Tahoma', 'Trebuchet ms', 'Verdana', 'sans-serif', 'Helvetica');

foreach ($fonts as $font) {
    if ($font == $p_row['user_font']) {
        $sel    = 'SELECTED';
    } else {
        $sel    = '';
    }
    $template->assign_block_vars('fonts', array(
        'NAME'      => $font,
        'SELECTED'  => $sel
    ));
}

for ($i = 8; $i<32; $i++) {
    if ($p_row['user_size'] == $i) {
        $sel    = 'SELECTED';
    } else {
        $sel    = '';
    }
    $template->assign_block_vars('sizes', array(
        'SIZE'      => $i,
        'SELECTED'  => $sel
    ));
}

if (isset($notice)) {
    $template->assign_block_vars('enotice', array('TEXT' => $notice));
}

$template->pparse('body');

